/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package codegen;

/**
 *
 * @author Felipe Gutierrez
 */


import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.java.dev.greenbox.base.Application;
import net.java.dev.greenbox.base.Clazz;
import net.java.dev.greenbox.base.GreenboxContext;
import net.java.dev.greenbox.core.util.Generator;
import net.java.dev.greenbox.core.util.source.generator.base.ClassGeneratorBase;
import net.java.dev.greenbox.exception.GenerationSourceException;
import utils.EntityUtils;
import utils.FormatationUtils;

public class GreenBoxProcessor {

	private boolean debug = true;
	public ArrayList<Clazz> clazzes;
	public void listTemplates() {

	}
	public GreenBoxProcessor(){
		this.clazzes = new ArrayList<Clazz>();
	}

	/**
	 *
	 * @param templates
	 * @param pathTemplate
	 * @param classes
	 * @param outDirPath
	 * @throws Exception
	 */
	public void execute(ArrayList<BlueTemplate> templates, String pathTemplate,
			Application app, String outDirPath) throws Exception {

		// ClassDecorator.buildMethodsFromAttributes(classe);
		EntityUtils entityUtils = new EntityUtils();
		for (BlueTemplate template : templates) {

			if (debug) {
				System.out.println("Generate: " + app.getClasses().size());
				System.out.println("  Template: " + template.getName());
			}
			if (template.getNameSubPackage() != null) {
				template.setNameSubPackage("." + template.getNameSubPackage());
			}
			filterClasses(app.getClasses(), app.getName());

			for (Clazz classe : this.clazzes) {

				if (debug) {
					System.out.println("     classe: " + classe.getClassName());

				}
				String fileName = template.getPrefixFileName()	+ classe.getClassName()	+ template.getSufixFileName()	+ "."
					+ template.getFileExtention();
				String packageName = getPackagePath(classe.getPackageName(), app.getName() );

				GreenboxContext gbc = new GreenboxContext();

				gbc.declareVariable("class", classe);
				gbc.declareVariable("utils", FormatationUtils.class);
				gbc.declareVariable("entityUtils", entityUtils);


				gbc.declareVariable("subPackage", template.getNameSubPackage());

				try {
					// Gerando os cÃ³digos baseando-se nos templates
					String input = ClassGeneratorBase.getInstance()
							.generateSourceByTemplateName(classe,
									template.getName(), gbc, pathTemplate);

					Generator.generateSource(packageName,
							outDirPath, template.getNameSubPackage(), fileName, input);

				} catch (GenerationSourceException ex) {
					Logger.getLogger(GreenBoxProcessor.class.getName()).log(
							Level.SEVERE, null, ex);
				} catch (IOException ex) {
					Logger.getLogger(GreenBoxProcessor.class.getName()).log(
							Level.SEVERE, null, ex);
				}
			}
		}
	}
	private void filterClasses(ArrayList<Clazz> classes, String appName) {
		int size = classes.size();
		for ( int i = 0; i < size; i++) {
			Clazz classe = classes.get(i);
			String[] packageName = classe.getPackageName().split("\\p{Punct}");

			if ( ( packageName[1].equals(appName)  ) ) {

				this.clazzes.add(classe);

			}
		}
	}

	private String getPackagePath(String packageName, String appName) {

		String[] auxModule = packageName.split("\\p{Punct}");

		String[] directoriesSRC = {"apps", appName, auxModule[2], "src"};

		StringBuilder builder = new StringBuilder();

		for( int i = 0; i < directoriesSRC.length; i++){

			builder.append(directoriesSRC[i] + ".");

		}
		builder.append(packageName);
		return builder.toString();
	}
}
